// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System;
using SnapObjects.Commons;

namespace SnapObjects.Data
{
    /// <summary>
    ///     Sets the search condition for the result set when a model class loads the data.
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false, Inherited = true)]
    public class SqlWhereAttribute : Attribute
    {
        /// <summary>
        ///     Initializes a new instance of the SqlWhereAttribute class.
        /// </summary>
        /// <param name="rawCondition">A raw SQL search condition in the WHERE clause after the keyword WHERE.</param>
        public SqlWhereAttribute(string rawCondition)
        {
            this.RawCondition = rawCondition;
            this.RawConditionDefined = true;

            Check.NotEmpty(rawCondition, nameof(rawCondition));
        }

        /// <summary>
        ///     Initializes a new instance of the SqlWhereAttribute class.
        /// </summary>
        /// <param name="left">
        ///     The raw SQL expression on the left of the operator.
        ///     If the operator property is not specified, the operator defaults to "=".
        /// </param>
        /// <param name="right">The raw SQL expression on the right of the operator.</param>
        public SqlWhereAttribute(string left, string right)
        {
            this.Left = left;
            this.Right = right;
            this.Operator = SqlBinaryOperator.Equals;

            Check.NotEmpty(left, nameof(left));
            Check.NotEmpty(right, nameof(right));
        }

        /// <summary>
        ///     Gets the string of the raw SQL search condition if it has been specified in the constructor.
        /// </summary>
        public string RawCondition { get; }

        /// <summary>
        ///     Gets the raw SQL expression on the left of the operator.
        /// </summary>
        public string Left { get; }

        /// <summary>
        ///     Gets or sets the operator used in the WHERE clause of the raw SQL.
        /// </summary>
        public SqlBinaryOperator Operator { get; set; }

        /// <summary>
        ///     Gets the raw SQL expression on the right of the operator.
        /// </summary>
        public string Right { get; }

        /// <summary>
        ///     Gets whether the raw search condition has been specified in the constructor.
        /// </summary>
        public bool RawConditionDefined { get; }
    }
}
